/*
 * Copyright (C) 2017 The MoonLake Authors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


package com.minecraft.moonlake.legendaryweaponrel.weapons.tool;

import com.minecraft.moonlake.MoonLakeAPI;
import com.minecraft.moonlake.api.packet.wrapper.PacketPlayOutChat;
import com.minecraft.moonlake.executor.Consumer;
import com.minecraft.moonlake.legendaryweaponrel.weapons.WeaponAbstractEntityDamageByEntity;
import com.minecraft.moonlake.legendaryweaponrel.weapons.WeaponCloseable;
import org.bukkit.Material;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import java.util.HashMap;
import java.util.Map;

public class OverKill extends WeaponAbstractEntityDamageByEntity implements WeaponCloseable {

    public OverKill() {
        super("overkill", "&b剔骨", Material.DIAMOND_SWORD);
    }

    @Override
    public String[] getLore() {
        return new String[] {
                "&7一击造成 &c4❤ &7伤害后进入 &a10s &7冷却时间",
                "&7并且自身获得 &a虚弱1 10s &7的负面效果"
        };
    }

    // 冷却时间 map
    private final static Map<String, Long> COOLDOWN_MAP = new HashMap<>();

    @Override
    protected void onDamage(EntityDamageByEntityEvent event, Player player, LivingEntity entity) {
        Long cache = COOLDOWN_MAP.get(player.getName());
        if(cache != null && System.currentTimeMillis() - cache < 10000) {
            // 冷却还未结束则阻止伤害
            event.setDamage(0d);
            return;
        }
        event.setDamage(0d); // 不要事件的伤害

        if(entity.getHealth() <= 8d) entity.setHealth(0d); // 血量低于 10 直接杀死
        else entity.setHealth(entity.getHealth() - 8d); // 否则当前生命减去

        // 玩家虚弱效果
        player.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, 10 * 20, 0));
        // 发送消息
        String cooldownEnter = getInstance().getMessage("WeaponCooldownEnter", displayName, 10);
        new PacketPlayOutChat(cooldownEnter, PacketPlayOutChat.Mode.HOTBAR).send(player);
        // 运行异步任务用来发送冷却完成的提示
        MoonLakeAPI.runTaskLaterAsync(getInstance(), () -> {
            if(!player.isOnline()) return; // 不在线则返回 (防止数据包抛出异常)
            String cooldownDone = getInstance().getMessage("WeaponCooldownDone", displayName);
            new PacketPlayOutChat(cooldownDone, PacketPlayOutChat.Mode.HOTBAR).send(player);
        }, 10 * 20L);
        // 将冷却时间 put 到 map 缓存
        COOLDOWN_MAP.put(player.getName(), System.currentTimeMillis());
    }

    // 玩家退出事件
    public Consumer<PlayerQuitEvent> onQuit(Player player) {
        return event -> {
            if(COOLDOWN_MAP.containsKey(player.getName()))
                COOLDOWN_MAP.remove(player.getName());
        };
    }

    @Override
    public void close() {
        // 关闭能力清除冷却时间缓存
        COOLDOWN_MAP.clear();
    }
}
